using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Xml;
using System.Reflection;

using System.Web.UI;
using System.Web.UI.WebControls;

using Pampa.CRUD.Configuration.Validations;
using Pampa.CRUD.Exceptions;
using Pampa.CRUD.UI.CustomControls;

namespace Pampa.CRUD.Configuration
{
    /// <summary>
    /// Esta clase representa la definicion de un textbox en el CRUD de una entidad
    /// </summary>

    public class TextItem : EditItem
    {
        int maxLength;
        int valueHeight;
        TextBoxMode textMode;
        Pampa.WebControls.TextBox textBox = new Pampa.WebControls.TextBox();

        public TextItem() : base()
        {
        }

        public TextItem(TextItem sourceItem) : base(sourceItem)
        {
            this.MaxLength = sourceItem.MaxLength;
            this.ValueHeight = sourceItem.ValueHeight;
            this.TextMode = sourceItem.TextMode;
        }
        
        #region Properties

        public int MaxLength
        {
            get { return maxLength; }
            set { maxLength = value; }
        }

        public int ValueHeight
        {
            get { return valueHeight; }
            set { valueHeight = value; }
        }

        public TextBoxMode TextMode
        {
            get { return textMode;}
            set { textMode = value; }
        }

        public override ItemType ItemType
        {
            get { return ItemType.TextBox; }
        }

        public override Type ValueTypeDefault
        {
            get { return typeof(System.String); }
        }

        public override Control Control
        {
            get
            {
                textBox.Width = this.ValueWidth;
                textBox.MaxLength = this.MaxLength;
                textBox.Attributes.Add("onchange", this.OnClientChange);
                textBox.TextMode = this.TextMode;
                textBox.ReadOnly = !this.Enabled;

                if (this.Value != null)
                {
                    if (Value is Double)
                    {
                        textBox.Text = ((double)this.Value).ToString(CultureInfo.InvariantCulture);
                    }
                    else if (Value is Decimal)
                    {
                        textBox.Text = ((Decimal)this.Value).ToString(CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        textBox.Text = this.Value.ToString();
                    }

                    textBox.Attributes.Add("value", this.Value.ToString());
                }

                if (this.TextMode == TextBoxMode.MultiLine)
                {
                    if (this.Enabled)
                        textBox.CssClass = "textArea";
                    else
                        textBox.CssClass = "textAreaDisabled";

                    textBox.Attributes.Add("onfocusin", "if(!this.readOnly) this.className = 'textAreaOnFocus';");
                    textBox.Attributes.Add("onfocusout", "if(!this.readOnly) this.className = 'textArea';");
                    textBox.Attributes.Add("onfocus", "if(!this.readOnly) this.className = 'textAreaOnFocus';");
                    textBox.Attributes.Add("onblur", "if(!this.readOnly) this.className = 'textArea';");

                    textBox.Height = Unit.Pixel(valueHeight);

                    // Parche para que ande el max length en text areas
                    if (maxLength > 0)
                    {
                        textBox.Attributes.Add("onkeypress", "return Common.imposeMaxLength(this, " + maxLength.ToString() + ");");
                        textBox.Attributes.Add("onkeydown", "Common.cutToMaxLength(this, " + maxLength.ToString() + ");");
                        textBox.Attributes.Add("onkeyup", "Common.cutToMaxLength(this, " + maxLength.ToString() + ");");
                    }
                }
                else
                {
                    if (this.Enabled)
                        textBox.CssClass = "textBox";
                    else
                        textBox.CssClass = "textBoxDisabled";

                    textBox.Attributes.Add("onfocusin", "if(!this.readOnly) this.className = 'textBoxOnFocus';");
                    textBox.Attributes.Add("onfocusout", "if(!this.readOnly) this.className = 'textBox';");
                    textBox.Attributes.Add("onfocus", "if(!this.readOnly) this.className = 'textBoxOnFocus';");
                    textBox.Attributes.Add("onblur", "if(!this.readOnly) this.className = 'textBox';");
                }

                return textBox;
            }
        }

        #endregion

        #region Public Functions

        public override VisibleItem Clone()
        {
            return new TextItem(this);
        }

        public override void LoadXml(XmlNode node)
        {
            try
            {
                base.LoadXml(node);

                if (node.Attributes["MaxLength"]!=null)
                    this.MaxLength = int.Parse(node.Attributes["MaxLength"].Value);
                if (node.Attributes["ValueHeight"] != null)
                    this.ValueHeight = int.Parse(node.Attributes["ValueHeight"].Value);
                if (node.Attributes["TextMode"] != null)
                    this.TextMode = (TextBoxMode)Enum.Parse(typeof(TextBoxMode), node.Attributes["TextMode"].Value);
            }
            catch (Exception ex)
            {
                throw new Pampa.CRUD.Exceptions.ConfigurationException("Item " + this.Id + " mal definido.", ex);
            }
        }

        #endregion
    }
}
